import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.net.*;
import java.util.*;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;
public class Client extends JFrame {
	private JPanel contentPane;
	private Socket socket;
	private JTextArea textArea;
	private JTextField textField;
	private JTextField textField1;
	private OutputStream out;
	private InputStream is;
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Client frame = new Client();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	public Client() throws UnknownHostException, IOException {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JPanel panel = new JPanel();
		panel.setBounds(10, 10, 398, 39);
		contentPane.add(panel);
		
		JLabel lblNewLabel = new JLabel("\u6635\u79F0\uFF1A");
		panel.add(lblNewLabel);
		
		textField = new JTextField();
		textField.setText("\u6E38\u5BA2");
		panel.add(textField);
		textField.setColumns(20);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(10, 59, 398, 143);
		contentPane.add(scrollPane);
		
		textArea = new JTextArea();
		scrollPane.setViewportView(textArea);
		
		JPanel panel1 = new JPanel();
		panel1.setBounds(10, 212, 398, 39);
		contentPane.add(panel1);
		
		JLabel lblNewLabel_1 = new JLabel("\u8F93\u5165\u804A\u5929\u5185\u5BB9");
		panel1.add(lblNewLabel_1);
		
		textField1 = new JTextField();
		panel1.add(textField1);
		textField1.setColumns(20);
		
		JButton btnNewButton = new JButton("\u53D1\u9001");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed1(ActionEvent e) {
				try{
					out=socket.getOutputStream();
					String name=textField.getText()+":";
					String text=name+textField1.getText()+"\n";
					out.write(text.getBytes());
					out.flush();
					textField1.setText("");
				}catch(IOException e1){
					e1.printStackTrace();
				}
			}

			public void actionPerformed(ActionEvent arg0) {
				
			}
		});
		btnNewButton.setHorizontalAlignment(SwingConstants.RIGHT);
		panel1.add(btnNewButton);
		
		socket=new Socket("127.0.0.1",8889);
		receiveMessage();
	}

	private void receiveMessage() {
		new Thread(){
			public void run(){
				try{
					is=socket.getInputStream();
					Scanner scan=new Scanner(is);
					while(socket.isConnected()){
						String text=scan.nextLine()+"\n";
						textArea.append(text);
						sleep(100);
					}
				}catch(Exception e){
					e.printStackTrace();
				}
			}
		}.start();
	}
}